package cn.gzmli.dtt.common.controll;

import cn.gzmli.dtt.common.annotation.Comment;
import cn.gzmli.dtt.common.annotation.Privilege;
import cn.gzmli.dtt.common.entity.Department;
import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.service.DepartmentService;
import cn.gzmli.dtt.common.utils.BaseController;
import cn.gzmli.dtt.common.utils.ComMapObjCompatator;
import cn.gzmli.dtt.common.utils.Constant;
import cn.gzmli.dtt.sbzyc.validation.Add;
import cn.gzmli.dtt.sbzyc.validation.Delete;
import cn.gzmli.dtt.sbzyc.validation.Update;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Descript 部门相关接口
 * @Author xsx
 * @Date 2017/12/25
 */
//@Controller
@RestController
@RequestMapping("/department")
public class DepartmentController implements BaseController<Department> {
    @Autowired
    private DepartmentService departmentService;

//    @Autowired
//    private IMPBusinessRMI businessServiceRMI;

    @Override
    @RequestMapping("/list")
    @ResponseBody
    @Comment("获取部门列表")
    public Result list() throws Exception {
        return new Result(1, "获取部门列表成功", this.departmentService.getAll());
    }

    @Override
    @RequestMapping("/listByCondition")
    @ResponseBody
    @Comment("获取部门列表")
    public Result listByCondition(HttpServletRequest request, PageInfo pageInfo) throws Exception {

        List<Map<String,Object>> data = this.departmentService.getByCondition(request, pageInfo);

        return new Result(1, "获取部门列表成功", data, pageInfo);
    }

    @Override
    @RequestMapping("/get")
    @ResponseBody
    @Comment("获取部门信息")
    public Result get(Integer id) throws Exception {
        return new Result(1, "获取部门信息成功", this.departmentService.get(id));
    }

    @Override
    @RequestMapping("/getByCondition")
    @ResponseBody
    @Comment("获取部门信息")
    public Result getByCondition(HttpServletRequest request) throws Exception {
        Map<String, String> condition = new HashMap<>();
        return new Result(1, "获取部门信息成功", this.departmentService.getByCondition(condition));
    }

    @Override
    @RequestMapping("/save")
    @ResponseBody
    @Comment("新增部门信息")
    @Privilege("/department/save")
    public Result save(@Validated(Add.class) Department department) throws Exception {
        return new Result(Constant.SUCCESS, "新增部门信息成功", this.departmentService.save(department));
    }

    @Override
    @RequestMapping("/update")
    @ResponseBody
    @Comment("修改部门信息")
    @Privilege("/department/update")
    public Result update(@Validated(Update.class)Department department) throws Exception {
        this.departmentService.update(department);
        return new Result(Constant.SUCCESS, "修改部门信息成功");
    }

//    @RequestMapping("/updateSite")
//    @ResponseBody
//    @Comment("更新机构隧道信息")
//    @Privilege("/department/updateSite")
//    public Result updateSite(Integer orgId, String siteIDs, Integer authObjType) throws Exception {
//        Result r = businessServiceRMI.addOrChangeUserSitesForAuth(orgId,siteIDs,authObjType);
//        if (r.getStatus()==Constant.SUCCESS) {
//            departmentService.updateSite(orgId, siteIDs);
//            return new Result(Constant.SUCCESS, "更新机构隧道信息成功！");
//        } else {
//            return new Result(Constant.FAIL, "更新机构隧道信息失败！");
//        }
//    }

    @RequestMapping("/getOrgSiteTree")
    @ResponseBody
    @Comment("获取机构隧道信息")
    public Result getOrgSiteTree(Integer orgId) throws Exception {
        return new Result(Constant.SUCCESS, "获取机构隧道信息成功", departmentService.getOrgSiteTree(orgId));
    }

//    @RequestMapping("/listBaseDatasForTree")
//    @ResponseBody
//    @Comment("获取机构隧道信息")
//    public Result listBaseDatasForTree(Integer listBaseDatasForTree) throws Exception {
//        Result r = businessServiceRMI.listBaseDatasForTree(listBaseDatasForTree);
//        return new Result(Constant.SUCCESS, "获取机构隧道信息成功", r);
//    }

    @Override
    @RequestMapping("/delete")
    @ResponseBody
    @Comment("删除部门信息")
    @Privilege("/department/delete")
    public Result delete(@Validated(Delete.class)Department department) throws Exception {
        this.departmentService.delete(department);
        return new Result(Constant.SUCCESS, "删除部门信息成功");
    }

    @RequestMapping("/listTree")
    @ResponseBody
    @Ignore
    public Result listTree(HttpServletRequest request) throws Exception {
        List<Map<String, Object>> data = this.departmentService.getTree(request);
        Collections.sort(data, new ComMapObjCompatator("orderNo"));
        return new Result(Constant.SUCCESS, "获取部门列表成功", data);
    }

    @Comment("获取合同供货单位")
    @RequestMapping("/listOrgTree")
    @ResponseBody
    @Ignore
    public Result listOrgTree(HttpServletRequest request) throws Exception {
        List<Map<String, Object>> data = this.departmentService.listOrgTree(request);
        Collections.sort(data, new ComMapObjCompatator("orderNo"));
        return new Result(Constant.SUCCESS, "获取部门列表成功", data);
    }

	@Override
	public Result lists(HttpServletRequest request, PageInfo pageInfo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
